/*************************************************************
 * File: Persistence.java
 * Copyright (c) 2012
 * Author: Huang Shaobin
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Feb 20, 2013
 * Description: 
 ************************************************************/

package snow.biz.basic.model;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.lang.ObjectUtils;

import snow.biz.system.model.User;
import snow.core.enums.Status;
import snow.core.exceptions.BusinessException;
import snow.core.persistence.Entity;
import snow.core.toolkit.Validator;
import snow.core.toolkit.format.Formatter;

/**
 * 持久化对象的基类
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
@SuppressWarnings("serial")
public class Persistence<ID extends Serializable> implements Entity<ID>, Serializable {

	private ID id; // 数据库主键
	private User createUser; // 记录创建人
	private Date createTime; // 记录创建时间
	private User modifyUser; // 记录修改人
	private Date modifyTime; // 记录修改人时间
	private Status status; // 记录状态

	protected Persistence() {
	}

	public Persistence(User createUser) throws BusinessException {
		this(createUser, Status.FAILURE);
	}

	public Persistence(User createUser, Status status) throws BusinessException {
		Validator.notNull(createUser, "200002", "failure.input.create.user.not.null");
		this.createTime = new Date();
		this.createUser = createUser;
		setModifyTime(this.createTime);
		setModifyUser(this.createUser);
		setStatus(status);
	}

	public boolean equals(Object obj) {
		if (obj instanceof Persistence) {
			Persistence<?> persistence = (Persistence<?>) obj;
			return ObjectUtils.equals(getId(), persistence.getId());
		}
		return false;
	}

	@Formatter(format = "yyyy-MM-dd HH:mm:ss")
	public Date getCreateTime() {
		return this.createTime;
	}

	@Formatter(serialize = false)
	public User getCreateUser() {
		return this.createUser;
	}

	@Formatter(name = "createUser")
	public String getCreateUserLoginName() {
		return this.createUser == null ? null : this.createUser.getLoginName();
	}

	public ID getId() {
		return this.id;
	}

	@Formatter(format = "yyyy-MM-dd HH:mm:ss")
	public Date getModifyTime() {
		return this.modifyTime;
	}

	@Formatter(serialize = false)
	public User getModifyUser() {
		return this.modifyUser;
	}

	@Formatter(name = "modifyUser")
	public String getModifyUserLoginName() {
		return this.modifyUser == null ? null : this.modifyUser.getLoginName();
	}

	public Status getStatus() {
		return this.status;
	}

	public int hashCode() {
		return ObjectUtils.hashCode(getId());
	}

	public void setCreateTime(Date createTime) {
	}

	public void setCreateUser(User createUser) {
	}

	public void setCreateUserLoginName(String createUserLoginName) {
	}

	public void setId(ID id) {
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	public void setModifyUser(User modifyUser) {
		this.modifyUser = modifyUser;
	}

	public void setModifyUserLoginName(String modifyUserLoginName) {
	}

	public void setStatus(Status status) throws BusinessException {
		Validator.notNull(status, "200003", "failure.input.status.not.null");
		this.status = status;
	}

	public String toString() {
		return getClass().getName() + '@' + getId();
	}

}
